package org.september.pisces.user.permission;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.september.pisces.module.api.PiscesModule;
import org.september.pisces.module.api.WebMenu;
import org.september.pisces.user.permission.controller.DepartmentController;
import org.september.pisces.user.permission.controller.OperationLogController;
import org.september.pisces.user.permission.controller.PiscesSystemRoleController;
import org.september.pisces.user.permission.entity.SystemUser;
import org.september.pisces.user.permission.service.DeptService;
import org.september.pisces.user.permission.service.SystemUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;


/**
 * 标注service，用于主工程发现插件模块
 * @author yexinzhou
 *
 */
@Configuration
@ComponentScan("org.september.pisces.user.permission")
public class UserPermissionModule implements PiscesModule {

	public static final String 用户与权限="用户与权限";
	public static final String 操作记录="操作记录";
	
	private static Map<String,List<WebMenu>> moduleMap = new HashMap<>();
	
	@Autowired
	private SystemUserService systemUserService;
	
	@Autowired
	private DeptService deptService;
	
	static {
		List<WebMenu> menus1 = new ArrayList<>();
//		menus1.add(new WebMenu("用户与权限-用户管理", PiscesSystemUserController.List_Page, "layui-icon-user")
//				.addPermission("列表页面", PiscesSystemUserPermission.用户列表.getUrls())
//				.addPermission("添加用户", PiscesSystemUserPermission.添加用户.getUrls())
//				.addPermission("编辑用户", PiscesSystemUserPermission.编辑用户.getUrls())
//				.addPermission("删除用户", PiscesSystemUserPermission.删除用户.getUrls()));
		
		menus1.add(new WebMenu("用户与权限-组织架构", DepartmentController.List_Page, "layui-icon-user")
				.addPermission("组织架构", PiscesSystemUserPermission.组织架构页面.getUrls())
				.addPermission("添加部门", PiscesSystemUserPermission.添加部门.getUrls())
				.addPermission("编辑部门", PiscesSystemUserPermission.编辑部门.getUrls())
				.addPermission("删除部门", PiscesSystemUserPermission.删除部门.getUrls())
				.addPermission("列表页面", PiscesSystemUserPermission.用户列表.getUrls())
				.addPermission("添加用户", PiscesSystemUserPermission.添加用户.getUrls())
				.addPermission("编辑用户", PiscesSystemUserPermission.编辑用户.getUrls())
				.addPermission("删除用户", PiscesSystemUserPermission.删除用户.getUrls()));
		
		menus1.add(new WebMenu("用户与权限-角色管理", PiscesSystemRoleController.List_Page, "layui-icon-user")
				.addPermission("列表页面", PiscesSystemUserPermission.角色列表.getUrls())
				.addPermission("添加角色", PiscesSystemUserPermission.添加角色.getUrls())
				.addPermission("编辑角色", PiscesSystemUserPermission.编辑角色.getUrls())
				.addPermission("分配权限", PiscesSystemUserPermission.分配权限.getUrls())
				.addPermission("克隆角色", PiscesSystemUserPermission.克隆角色.getUrls())
				.addPermission("删除角色", PiscesSystemUserPermission.删除角色.getUrls()));
		
		moduleMap.put(用户与权限, menus1);
		
		List<WebMenu> menus3 = new ArrayList<>();
		menus3.add(new WebMenu("操作记录-操作日志", OperationLogController.List_Page, "layui-icon-user")
				.addPermission("日志列表", PiscesSystemUserPermission.操作日志列表.getUrls()));
		moduleMap.put(操作记录, menus3);
	}
	
	public static List<WebMenu> getWebMenus(String moduleName) {
		return moduleMap.get(moduleName);
    }
	
	public UserPermissionModule(){
		System.out.println(this.getClass().getName()+" init..");
	}

	public String[] getMyBatisMapperLocation() {
		return new String[] {"classpath:"+this.getClass().getPackageName().replace(".", "/")+"/mapper/*.xml"};
	}
	
	public String[] getEntityPackages() {
		return new String[] {this.getClass().getPackageName()+".entity"};
    }

	public void init() {
		//初始化用户
		SystemUser su = new SystemUser();
		su.setUsername("superadmin");
		su.setPassword("Admin@123");
		su.setRealname("superadmin");
		SystemUser po = systemUserService.getUserByUserName("superadmin");
		if(po==null) {
			systemUserService.addUser(su);
		}
		
		deptService.initTopDept();
    }
}
